What is kleur?
The kleur npm package is a library for formatting terminal text with ANSI colors. It provides a simple and chainable API to style strings that are output to the console with various colors, backgrounds, and text styles.
What are kleur's main functionalities?
Coloring text
This feature allows you to apply text color to your console output. The example shows how to color the text green.
const kleur = require('kleur');
console.log(kleur.green('Hello world!'));
Chaining styles
Kleur supports chaining multiple styles together. In this example, the text 'Error!' is styled to be red, bold, and underlined.
const kleur = require('kleur');
console.log(kleur.red().bold().underline('Error!'));
Background colors
You can also set background colors for your text. Here, the text 'Info' has a blue background with white foreground color.
const kleur = require('kleur');
console.log(kleur.bgBlue().white('Info'));
Conditional styling
Kleur allows conditional styling, where you can enable or disable colors based on certain conditions. In this example, the red color is applied only if the environment is not production.
const kleur = require('kleur');
const enabled = process.env.NODE_ENV !== 'production';
console.log(kleur.enabled(enabled).red('Only styled if not in production'));
Other packages similar to kleur
chalk
Chalk is a popular npm package similar to kleur that allows for styling terminal strings. It offers a more extensive API and additional features like template literal support and custom themes, but it is slightly larger in size compared to kleur.
colors
Colors is another package that provides ANSI color formatting for text in the terminal. It extends String.prototype to add color properties, which some developers may find less clean than the functional approach taken by kleur.
ansi-colors
Ansi-colors is a lightweight package that focuses on performance. Like kleur, it does not extend String.prototype and has a chainable API, but it has fewer dependencies and is designed to be as minimal as possible.
The fastest Node.js library for formatting terminal text with ANSI colors~!
Features
Originally inspired by ansi-colors
. See Credits for more info!
Install
$ npm install --save kleur
Usage
const kleur = require('kleur');
kleur.red('red text');
kleur.blue.bold.underline('howdy partner');
kleur.bold(`${ kleur.bgRed.white('[ERROR]') } ${ kleur.red.italic('Something happened')}`);
Chained Methods
console.log(kleur.bold.red('this is a bold red message'));
console.log(kleur.bold.italic('this is a bold italicized message'));
console.log(kleur.bold.yellow.bgRed.italic('this is a bold yellow italicized message'));
console.log(kleur.green.bold.underline('this is a bold green underlined message'));
Nested Methods
const { yellow, red, cyan } = require('kleur');
console.log(yellow(`foo ${red.bold('red')} bar ${cyan('cyan')} baz`));
console.log(yellow('foo ' + red.bold('red') + ' bar ' + cyan('cyan') + ' baz'));
Conditional Support
Toggle color support as needed; kleur
assumes it's always enabled.
const kleur = require('kleur');
kleur.enabled = false;
kleur.enabled = require('color-support').level;
console.log(kleur.red('I will only be colored red if the terminal supports colors'));
API
Any kleur
method returns a String
(when invoked, not chained). It's up to the developer to pass the output to destinations like console.log
, process.stdout.write
, etc.
The methods below are grouped by type for legibility purposes only. They each can be chained or nested with one another.
Colors:
black — red — green — yellow — blue — magenta — cyan — white — gray
Backgrounds:
bgBlack — bgRed — bgGreen — bgYellow — bgBlue — bgMagenta — bgCyan — bgWhite
Modifiers:
reset — bold — dim — italic* — underline — inverse — hidden — strikethrough*
* Not widely supported
Benchmarks
Using Node v8.9.0
Load time
chalk: 9.372ms
turbocolor: 0.526ms
ansi-colors: 0.851ms
kleur: 0.862ms
Performance
# All Colors
ansi-colors x 60,485 ops/sec ±0.63% (96 runs sampled)
chalk x 7,184 ops/sec ±3.77% (68 runs sampled)
turbocolor x 95,468 ops/sec ±0.60% (94 runs sampled))
kleur x 151,365 ops/sec ±0.22% (95 runs sampled)
# Stacked colors
ansi-colors x 13,754 ops/sec ±0.44% (93 runs sampled)
chalk x 1,732 ops/sec ±3.76% (71 runs sampled)
turbocolor x 28,709 ops/sec ±1.32% (92 runs sampled)
kleur x 30,837 ops/sec ±0.13% (93 runs sampled)
# Nested colors
ansi-colors x 28,898 ops/sec ±0.32% (96 runs sampled)
chalk x 3,389 ops/sec ±4.03% (71 runs sampled)
turbocolor x 48,034 ops/sec ±1.47% (99 runs sampled)
kleur x 61,266 ops/sec ±0.33% (97 runs sampled)
Credits
This project was originally inspired by Brian Woodward's awesome ansi-colors
project.
Unlike v1, the latest version(s) of kleur
no longer supports:
- printf-formatting
- variadic function arguments
- multiline text via
\n
or \r
kleur.clear()
method
In addition, kleur
continues to be ship without symbols and bright color variants.
If you need any of these features, please use ansi-colors
instead~!
License
MIT © Luke Edwards